/**
 *  golive
 *
 *  Create by songli on 2021/06/08
 *  Copyright © 2021 居理买房网. All rights reserved.
 */

package model

import (
	"fmt"
	"strconv"
)

const (
	urlShardingNum int = 4
)

type UrlMainShard struct {
	Id             int32  `gorm:"primary_key" redis:"id"`
	ShortUrl       string `redis:"short_url,omitempty"`
	LongUrl        string `redis:"long_url,omitempty"`
	CreateDatetime int64  `redis:"create_datetime,omitempty"`

	ShardId     int32  `redis:"shard_id,omitempty"`
	UrlMainFlag string `redis:"url_main_flag,omitempty"`
}

func (m UrlMainShard) TableName() string {
	return "u_url_main" + "_" + m.UrlMainFlag + "_" + strconv.Itoa(int(m.ShardId)%urlShardingNum)
}

func (m *UrlMainShard) SetMainFlag(flag string) error {
	if !CheckMainFlags(flag) {
		return fmt.Errorf("%s is illegal,please set dbs map.", flag)
	}
	m.UrlMainFlag = flag
	return nil
}
